<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use DB;
// use function GuzzleHttp\Psr7\readline;

class TestDataController extends Controller
{

    
    //试验数据修改
    public function edit(Request $request)
    {
        if ($request->isMethod('POST')) {
            // dd( $request->all());
            //update 返回受影响的行数
            $data_val = $request->only(['wendu', 'shidu', 'test_voltage', 'test_time', 'test_leakagecurrent']);
            $data_detail = $request->only(['test_date', 'next_date', 'jielun']);
            $result_val = DB::table($request->table_name_val)->where('id', $request->data_test_id)->update($data_val);
            $result_detail = DB::table('a_contract_detail')->where('id', $request->id)->update($data_detail);
            //由于框架自身不支持响应bool值，所以转化一种形式
            return $result_val >= 0 && $result_detail >= 0 ? '1' : '0';
        } else {
            // dd($request->all());
            $id = $request->id;
            //获取样品的基础信息
            $data_basic = DB::table('a_contract_detail')->find($id);
            $sample_name = $data_basic->sample_name;
            $bar_code = $data_basic->bar_code;
            //获取样品名 与 试验数据表、试验标准表对应关系的一条记录
            $data_vs = DB::table('a_sample_vs_valuetable')->where('sample_name', $sample_name)->first();
            //存储试验数据的表名
            $table_name_val = $data_vs->table_name_val;
            //取出试验数据
            $data_test = DB::table($table_name_val)->where('bar_code', $bar_code)->first();

            return view('admin.testData.' . $table_name_val, compact('data_test', 'data_basic', 'table_name_val'));
        }
    }
}
